//
//  YCRegisterSecondViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/1.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCRegisterSecondViewController.h"

#define ORGIN_X S(15)
#define BTN_HEIGHT S(50)
#define FIELD_HEIGHT S(50)
@interface YCRegisterSecondViewController ()
{
    BOOL _isForgetPassword;
    UITextField *m_codeField;
    UIButton *m_btnConfirm;
}
@end

@implementation YCRegisterSecondViewController

-(id)initWith:(BOOL)isResetPassword
{
    self=[super init];
    if (self) {
        _isForgetPassword=isResetPassword;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=_isForgetPassword ? @"找回密码" : @"注册";
    self.navigationItem.leftBarButtonItem=[self tbarBackButton];
    
    [self.view setBackgroundColor:WHITE_COLOR];
    [self addMainView];
}
-(void)addMainView
{
    CGFloat fWidth=kMainScreen_Width-2*ORGIN_X;
    
    m_codeField=[[UITextField alloc]initWithFrame:CGRectMake(ORGIN_X, kNAV_HEIGHT+30, fWidth, FIELD_HEIGHT)];
    [m_codeField setFont:[UIFont LightFontOfSize:15]];
    [m_codeField setBackgroundColor:WHITE_COLOR];
    m_codeField.delegate=self;
    m_codeField.secureTextEntry=YES;
    m_codeField.clearButtonMode=UITextFieldViewModeWhileEditing;
    m_codeField.keyboardType = UIKeyboardTypeASCIICapable;
    [m_codeField setPlaceholder:@"请设置新密码（6位以上数字、字母）" Corlor:DISABLE_COLOR];
    [m_codeField addShadow];
    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kDistance, FIELD_HEIGHT)];
    m_codeField.leftView = leftView;
    m_codeField.leftViewMode = UITextFieldViewModeAlways;    
    [self.view addSubview:m_codeField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_codeField];
    
    m_btnConfirm=[[UIButton alloc]init];
    [m_btnConfirm.titleLabel setFont:[UIFont LightFontOfSize:18]];
    [m_btnConfirm setBackgroundColor:MAIN_COLOR];
    [m_btnConfirm setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [m_btnConfirm setTitle:@"完成修改" forState:UIControlStateNormal];
    m_btnConfirm.layer.cornerRadius=2.0f;
    [m_btnConfirm addTarget:self action:@selector(confirmBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m_btnConfirm];
    [m_btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kDistance);
        make.right.equalTo(self.view).offset(-kDistance);
        make.top.equalTo(m_codeField.bottom).offset(S(50));
        make.height.mas_equalTo(50);
    }];

    [self textChange];
}
-(void)textChange
{
    m_btnConfirm.enabled= (m_codeField.text.length>4);
    if (m_btnConfirm.enabled) {
        [m_btnConfirm setBackgroundColor:MAIN_COLOR];
    }else{
        [m_btnConfirm setBackgroundColor:LIGHT_COLOR];
    }
}

-(void)confirmBtnClicked:(UIButton *)button
{
    [m_codeField resignFirstResponder];
    UserFindPwdRequest *request=[UserFindPwdRequest new];
    request.phone=_phone;
    request.code=_code;
    request.password=m_codeField.text;
    [apiClient hideProgress];
    [apiClient doUserFindPwd:request success:^(ApiResponse *data, NSString *url) {        
        [self.view showHudWith:data.message];
        [self loadLogin];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)loadLogin
{
    UserLoginRequest *request = [UserLoginRequest new];
    request.phone = _phone;
    request.password = m_codeField.text;
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    [apiClient doUserLogin:request success:^(ApiResponse *data, NSString *url) {
        UserLoginResponse *response=[[UserLoginResponse alloc]initWithCGResponse:data];
        [App shared].user = response.data;
        [[App shared] save];
        [[App shared] restore];
        [[AppDelegate appDelegate]loadLogin];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
@end




